﻿using Coldairarrow.Business.Interface;
using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.Interface;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Math;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Security.Principal;
using System.Threading.Tasks;
using System.Xml.Linq;
using static AutoMapper.Internal.CollectionMapperExpressionFactory;

namespace Coldairarrow.Business.Base_Manage
{
    public class OL_AccountBusiness
    {
        public IDbAccessor Db { get; set; }
        /// <summary>
        /// 钉钉流程表单操作业务
        /// </summary>
        public DD_ProcessInterfaceBusiness _DD_ProcessInterfaceBusiness { get; set; }
        /// <summary>
        /// One_link资产台账表操作业务
        /// </summary>
        public AccountInterfaceBusiness _AccountInterfaceBusiness { get; set; }

        public AccountTypeInterfaceBusiness _AccountTypeInterfaceBusiness { get; set; }
        public OL_AccountBusiness(IDbAccessor db)
        {
            this.Db = db;
            _DD_ProcessInterfaceBusiness = new DD_ProcessInterfaceBusiness();
            _AccountInterfaceBusiness = new AccountInterfaceBusiness();
            _AccountTypeInterfaceBusiness = new AccountTypeInterfaceBusiness();

        }

        #region 采购
        /// <summary>
        /// 资产采购获取新增数据并新增到库存
        /// </summary>
        /// <param name="accountDTO"></param>
        /// <returns></returns>
        public async Task PurAccounts(AccountDTO accountDTO)
        {

            #region 获取钉钉的数据
            //根据单据实例ID查询钉钉的流程表单
            List<ProcessDTO> processDTOs = await _DD_ProcessInterfaceBusiness.QueryProcess(accountDTO);
            ProcessDTO process = processDTOs[0];

            //固定资产数据列表
            List<Account> addFixedItem = accountDTO.Account_Item.Where(x => x.Name != "").ToList();
            #endregion
            #region 获取One_link的数据
            //获取ONE_LINKD的资产类型
            List<AccountType> accountType = await _AccountTypeInterfaceBusiness.GetRows();

            //获取多物一码的物料数据更新
            //List<OL_FilterDTO> filters = new List<OL_FilterDTO>();
            //accountDTO.Account_Item.ForEach(x =>
            //{
            //    //获取物料的资产类型信息
            //    List<AccountType> types = accountType.Where(t => t.Type == x.Type).ToList();
            //    AccountType type = types.Count() > 0 ? types[0] : new AccountType();
            //    //设置ONE_link的查询条件：资产类型=Value的项
            //    if (type.Inventory == "多物一码")
            //    {
            //        OL_FilterDTO filter = new OL_FilterDTO() { ControlId = "6706509fa23bbf6d2b0d1ec9", DataType = 2, FilterType = 2, Value = x.Type, SpliceType = 2 };
            //        filters.Add(filter);
            //    }
            //});
            //List<Account> editAccounts = await _AccountInterfaceBusiness.QueryRow(filters);
            #endregion

            #region 获取固定资产新增，非固定资产新增项
            addFixedItem = PUR_GetAssetsAddRow(addFixedItem, process, accountType);
            #endregion
            //新增ONE_LINK数据
            await _AccountInterfaceBusiness.AddRow(addFixedItem);

        }

        /// <summary>
        /// 获取采购资产新增项业务逻辑
        /// </summary>
        /// <param name="accounts">钉钉新增的固定资产列表</param>
        /// <param name="process">钉钉业务流程单据</param>
        /// /// <param name="accountType">物料类型列表</param>
        /// <returns></returns>
        private List<Account> PUR_GetAssetsAddRow(List<Account> addAccounts, ProcessDTO process, List<AccountType> accountType, List<Account> editAccounts = null)
        {
            List<Account> accounts = new List<Account>();
            Account head = new Account();
            //获取单据体信息
            head.Center = process.process_instance.originator_dept_name;//发起部门
            process.process_instance.form_component_values.ForEach(x =>
            {
                head.DDAddBill = process.process_instance.business_id;//审批单号
                head.DDAddType = process.process_instance.title.GetStringAfterChar('的');//新建单据类型
                if (x.id == "DepartmentField-K8DT7SR4") head.Department = x.value == "null" ? "" : x.value;//部门
                if (x.id == "TextField_134L9VUWCURK0") head.SavePosition = x.value == "null" ? "" : x.value;//存放地点
                if (x.id == "InnerContactField_5TVJXVKRF240") head.User = x.value == "null" ? "" : x.value;//实际使用人
                if (x.id == "DDSelectField_19WGTFHRVVFK0") head.TypeT = x.value == "null" ? "" : x.value; ;//资产类型
                if (x.id == "DDSelectField_1IMEPNUKPNZ40")
                {
                    head.Org = x.value == "null" ? "" : x.value; ;//所属组织
                    if (head.Org.Contains("力")) head.OrgCode = "LK";//所属组织代码
                    else if (head.Org.Contains("法")) head.OrgCode = "FGD";
                    else if (head.Org.Contains("神")) head.OrgCode = "SJ";
                    else if (head.Org.Contains("台")) head.OrgCode = "MT";
                    else head.OrgCode = "N";
                }

            });
            //完善新增数据 
            foreach (var item in addAccounts)
            {
                //获取物料的资产类型信息
                List<AccountType> types = accountType.Where(t => t.Type == item.Type).ToList();
                AccountType type = types.Count() > 0 ? types[0] : new AccountType();
                //金额大于5千为固定资产需要一台一台新增，电脑组装配件也需要
                item.Price = item.Price.IsNullOrEmpty() ? "0" : item.Price;
                //资产代码
                item.AccountCode = type.Code.IsNullOrEmpty() ? "G" : type.Code;
                item.OrgCode = head.OrgCode;
                double price = item.Price.ToDouble();
                if (price >= 5000 || type.Inventory == "一物一码")
                {
                    //固定资产需要分开一个一个新增
                    if (item.Qty == "1")
                    {
                        PUR_UpdateFixedItem(item, head);

                    }
                    else
                    {
                        int qty = item.Qty.ToInt();
                        for (int i = 0; i < qty; i++)
                        {
                            if (i == 0)
                            {
                                item.Qty = "1";
                                PUR_UpdateFixedItem(item, head);
                            }
                            else
                            {
                                accounts.Add(item);
                            }
                        }
                    }
                }
                else
                {

                    PUR_UpdateNoFixedItem(item, head);
                }
                accounts.Add(item);
            }

            return accounts;
        }

        /// <summary>
        /// 固定资产、需要盘点资产的数据赋值逻辑
        /// </summary>
        /// <param name="item"></param>
        /// <param name="head"></param>
        /// <returns></returns>
        private Account PUR_UpdateFixedItem(Account item, Account head)
        {

            //获取单据体信息
            item.DDAddBill = head.DDAddBill;//审批单号
            item.DDAddType = head.DDAddType;//新建单据类型
            item.SavePosition = head.SavePosition;//存放地点
            item.TypeT = head.TypeT;//资产类型
            item.Org = head.Org;//组织
            if (item.Price.ToDouble() >= 5000)
            { item.IsFixed = "固定资产"; }
            else
            { item.IsFixed = "类固定资产"; }//是否是固定资产
            item.PurDate = DateTime.Now.ToString("yyyy-MM-dd");//采购入库日期
            if (item.State == "是")
            {
                item.Department = "信息化部";//部门
                item.State = "闲置";
            }//状态
            else
            {
                item.Center = head.Center;//中心
                item.Department = head.Department;//部门
                item.User = head.User;//实际使用人
                item.State = "在用";
                item.GetDate = DateTime.Now.ToString("yyyy-MM-dd");//领用日期
            }
            return item;
        }


        /// <summary>
        /// 非固定资产的数据赋值逻辑
        /// </summary>
        /// <param name="item"></param>
        /// <param name="head"></param>
        /// <returns></returns>
        private Account PUR_UpdateNoFixedItem(Account item, Account head)
        {

            //获取单据体信息
            item.DDAddBill = head.DDAddBill;//审批单号
            item.DDAddType = head.DDAddType;//新建单据类型
            item.SavePosition = head.SavePosition;//存放地点
            item.TypeT = head.TypeT;//资产类型
            item.Org = head.Org;//组织
            item.IsFixed = "类固定资产";
            item.PurDate = DateTime.Now.ToString("yyyy-MM-dd");//采购入库日期
            //根据是否备用修改状态
            if (item.State == "是")
            {
                item.State = "闲置";
            }//状态
            else
            {
                item.Center = head.Center;//中心
                item.Department = head.Department;//部门
                item.User = head.User;//实际使用人
                item.State = "在用";
                //item.Qty = "0";
                item.GetDate = DateTime.Now.ToString("yyyy-MM-dd");//领用日期
            }
            return item;
        }
        #endregion


        #region 调拨
        /// <summary>
        /// 资产调拨新增/更新数据的业务逻辑
        /// </summary>
        /// <param name="accountDTO">请求数据</param>
        /// <returns></returns>
        public async Task TransferAccounts(AccountDTO accountDTO)
        {

            List<OL_FilterDTO> filters = new List<OL_FilterDTO>();
            accountDTO.Account_Item.ForEach(x =>
            {
                //设置ONE_link的查询条件：资产编码=Value的项
                OL_FilterDTO filter = new OL_FilterDTO() { ControlId = "6709cb88a23bbf6d2b165ec1", DataType = 2, FilterType = 2, Value = x.AccountNO, SpliceType = 2 };
                filters.Add(filter);
                //设置ONE_link的查询条件：关联主机的值=Value的项
                //filter = new OL_FilterDTO() { ControlId = "670f6e80a23bbf6d2b2a9194", DataType = 2, FilterType = 2, Value = x.AccountNO, SpliceType = 2 };
                //filters.Add(filter);
            });
            //根据资产编码查询对应的数据获取ROWID
            List<Account> accounts = await _AccountInterfaceBusiness.QueryRow(filters);
            //获取ONE_LINKD的资产类型
            List<AccountType> accountType = await _AccountTypeInterfaceBusiness.GetRows();

            //根据单据实例ID查询钉钉的流程表单
            List<ProcessDTO> pocessDTO = await _DD_ProcessInterfaceBusiness.QueryProcess(accountDTO);
            ProcessDTO process = pocessDTO[0];
            //数据获取成功
            if (process.errmsg == "ok" && accountDTO.Account_Item.Count > 0)
            {
                //更新或新增资产台账的业务逻辑
                List<Account> Accounts = TRS_GetAssetsEditRow(accounts, accountDTO.Account_Item, process, accountType);
                var addRow = Accounts.Where(x => x.UnpackingFlag == "new").ToList();
                var updateRow = Accounts.Where(x => x.UnpackingFlag.IsNullOrEmpty()).ToList();
                //更新ONE_LINK数据
                await _AccountInterfaceBusiness.EditRow(updateRow);
                await _AccountInterfaceBusiness.AddRow(addRow);
            }
        }
        /// <summary>
        /// 调拨流程-获取固定资产更新/新增项
        /// </summary>
        /// <param name="accountDTO">请求数据</param>
        /// <returns></returns>
        private List<Account> TRS_GetAssetsEditRow(List<Account> OL_accounts, List<Account> DD_accounts, ProcessDTO process, List<AccountType> accountType)
        {
            List<Account> Accounts = new List<Account>();
            Account head = new Account();
            //单体控件的数据
            head.DDEditBill = process.process_instance.business_id;//单据编号;
            head.DDEditType = process.process_instance.title.GetStringAfterChar('的');//新建单据类型;
            process.process_instance.form_component_values.ForEach(c =>
            {
                //根据调拨类型修改物料状态
                if (c.id == "DDSelectField_1XEE5PJ0BM3K0") head.State = c.value;//单据类型
                if (c.id == "InnerContactField_WL2FV5TBB6O0") head.User = c.value;//接收人
                if (c.id == "TextField_1EDBOKLP1UIO0") head.User_Put = c.value;//调出人
                if (c.id == "TextField_4YLBM5BQJ3W0") head.SavePosition = c.value;//存放地点
                if (c.id == "DepartmentField_1MCRD2P2OPB40") head.Department = c.value;//存放部门
                if (c.id == "DDSelectField_1A5QEVZGQ1XC0") head.Org = c.value;//存放部门
            });


            FolwType folwType = FolwType.其他;
            if (head.State == "领用")
            {
                Accounts = Neck(OL_accounts, DD_accounts, head, accountType);
                folwType = FolwType.领用;
            }
            else if (head.State == "借用")
            {
                Accounts = Borrow(OL_accounts, DD_accounts, head, accountType);
                folwType = FolwType.借用;
            }
            else if (head.State == "调拨")
            {
                Accounts = Transfer(OL_accounts, DD_accounts, head, accountType);
                folwType = FolwType.调拨;
            }
            else if (head.State == "归还")
            {
                Accounts = Return(OL_accounts, DD_accounts, head, accountType);
                folwType = FolwType.归还;

            }
            else if (head.State == "维修")
            {
                Accounts = Maintenance(OL_accounts, DD_accounts, head, accountType);
                folwType = FolwType.维修;
            }
            else if (head.State == "报废")
            {
                Accounts = Return(OL_accounts, DD_accounts, head, accountType);
                folwType = FolwType.报废;
            }
            if (Accounts.Count > 0) AddFolw(Accounts.Where(x => x.UnpackingFlag.IsNullOrEmpty()).ToList(), folwType, head);
            return Accounts;
        }


        /// <summary>
        /// 领用的业务逻辑
        /// </summary>
        private List<Account> Neck(List<Account> OL_accounts, List<Account> DD_accounts, Account head, List<AccountType> accountType)
        {
            List<Account> Accounts = new List<Account>();
            //获取更新的数据
            OL_accounts.ForEach(account =>
            {
                //获取物料的资产类型信息
                List<AccountType> types = accountType.Where(t => t.Type == account.Type).ToList();
                AccountType type = types.Count() > 0 ? types[0] : new AccountType();
                var dd_item = DD_accounts.Where(x => x.AccountNO == account.AccountNO).First();
                account.DDEditBill = head.DDEditBill;//单据编号;
                account.DDEditType = head.DDEditType;//新建单据类型;
                account.Org = head.Org;//组织
                //如果需求数量与库存数量相等，不拆箱直接更新数据，否则插箱
                if (account.Qty.ToDouble() == dd_item.Qty.ToDouble())
                {
                    //一物一码的物操作数量与库存数量不相等需要拆箱，多物一码的不需要
                    if (type.Inventory == "一物一码")
                    {
                        account.State = "在用";
                        account.User = head.User;//接收人
                        account.Department = head.Department.IsNullOrEmpty() ? "" : head.Department;//存放部门
                        account.GetDate = DateTime.Now.ToString("yyyy-MM-dd");//领用日期
                        account.SavePosition = head.Department.IsNullOrEmpty() ? "" : head.SavePosition;//存放地点
                        Accounts.Add(account);
                    }
                    else
                    {
                        account.Qty = (account.Qty.ToDouble() - dd_item.Qty.ToDouble()).ToString();
                        Accounts.Add(account);
                    }
                }
                else
                {
                    //一物一码的物操作数量与库存数量不相等需要拆箱，多物一码的不需要
                    if (type.Inventory == "一物一码")
                    {
                        //需要操作的箱号数据
                        Account item = TRS_Unpacking(account, dd_item.Qty, head);
                        Accounts.Add(item);

                        //剩余数量的箱号数据
                        account.Qty = (account.Qty.ToDouble() - dd_item.Qty.ToDouble()).ToString();
                        account.MainBox = account.AccountNO;//记录主箱号
                        account.RowId = "";
                        account.AccountNO = "";
                        account.UnpackingFlag = "new";
                        if (account.UnpackingFlag == "new") Accounts.Add(account);
                    }
                    else
                    {
                        account.Qty = (account.Qty.ToDouble() - dd_item.Qty.ToDouble()).ToString();
                        Accounts.Add(account);
                    }

                }

            });
            return Accounts;

        }

        /// <summary>
        /// 调拨的业务逻辑
        /// </summary>
        private List<Account> Transfer(List<Account> OL_accounts, List<Account> DD_accounts, Account head, List<AccountType> accountType)
        {
            List<Account> Accounts = new List<Account>();
            //获取更新的数据
            OL_accounts.ForEach(account =>
            {
                //获取物料的资产类型信息
                List<AccountType> types = accountType.Where(t => t.Type == account.Type).ToList();
                AccountType type = types.Count() > 0 ? types[0] : new AccountType();
                var dd_item = DD_accounts.Where(x => x.AccountNO == account.AccountNO).First();
                account.DDEditBill = head.DDEditBill;//单据编号;
                account.DDEditType = head.DDEditType;//新建单据类型;
                account.Org = head.Org;//组织
                //如果需求数量与库存数量相等，不拆箱直接更新数据，否则插箱
                if (account.Qty.ToDouble() == dd_item.Qty.ToDouble())
                {
                    //一物一码的物操作数量与库存数量不相等需要拆箱，多物一码的不需要
                    if (type.Inventory == "一物一码")
                    {
                        account.State = "在用";
                        account.User = head.User;//接收人
                        account.Department = head.Department.IsNullOrEmpty() ? "" : head.Department;//存放部门
                        account.GetDate = DateTime.Now.ToString("yyyy-MM-dd");//领用日期
                        account.SavePosition = head.Department.IsNullOrEmpty() ? "" : head.SavePosition;//存放地点
                        Accounts.Add(account);
                    }
                    else
                    {
                        account.Qty = (account.Qty.ToDouble() - dd_item.Qty.ToDouble()).ToString();
                        Accounts.Add(account);
                    }
                }
                else
                {
                    //一物一码的物操作数量与库存数量不相等需要拆箱，多物一码的不需要
                    if (type.Inventory == "一物一码")
                    {
                        //需要操作的箱号数据
                        Account item = TRS_Unpacking(account, dd_item.Qty, head);
                        Accounts.Add(item);

                        //剩余数量的箱号数据
                        account.Qty = (account.Qty.ToDouble() - dd_item.Qty.ToDouble()).ToString();
                        account.MainBox = account.AccountNO;//记录主箱号
                        account.RowId = "";
                        account.AccountNO = "";
                        account.UnpackingFlag = "new";
                        if (account.UnpackingFlag == "new") Accounts.Add(account);
                    }
                    else
                    {
                        account.Qty = (account.Qty.ToDouble() - dd_item.Qty.ToDouble()).ToString();
                        Accounts.Add(account);
                    }

                }

            });
            return Accounts;

        }
        /// <summary>
        /// 借用的业务逻辑
        /// </summary>
        private List<Account> Borrow(List<Account> OL_accounts, List<Account> DD_accounts, Account head, List<AccountType> accountType)
        {
            List<Account> Accounts = new List<Account>();
            //获取更新的数据
            OL_accounts.ForEach(account =>
            {
                //获取物料的资产类型信息
                List<AccountType> types = accountType.Where(t => t.Type == account.Type).ToList();
                AccountType type = types.Count() > 0 ? types[0] : new AccountType();
                var dd_item = DD_accounts.Where(x => x.AccountNO == account.AccountNO).First();
                account.DDEditBill = head.DDEditBill;//单据编号;
                account.DDEditType = head.DDEditType;//新建单据类型;
                account.Org = head.Org;//组织
                //如果需求数量与库存数量相等，不拆箱直接更新数据，否则插箱
                if (account.Qty.ToDouble() == dd_item.Qty.ToDouble())
                {
                    if (type.Inventory == "一物一码")
                    {
                        account.State = "借用";
                        account.User = head.User;//接收人
                        account.Department = head.Department.IsNullOrEmpty() ? "" : head.Department;//存放部门
                        account.GetDate = DateTime.Now.ToString("yyyy-MM-dd");//领用日期
                        account.SavePosition = head.Department.IsNullOrEmpty() ? "" : head.SavePosition;//存放地点
                        Accounts.Add(account);
                    }
                    else
                    {
                        account.Qty = (account.Qty.ToDouble() - dd_item.Qty.ToDouble()).ToString();
                        Accounts.Add(account);
                    }
                }
                else
                {

                    //一物一码的物操作数量与库存数量不相等需要拆箱，多物一码的不需要
                    if (type.Inventory == "一物一码")
                    {
                        //需要操作的箱号数据
                        Account item = TRS_Unpacking(account, dd_item.Qty, head);
                        Accounts.Add(item);

                        //剩余数量的箱号数据
                        account.Qty = (account.Qty.ToDouble() - dd_item.Qty.ToDouble()).ToString();
                        account.MainBox = account.AccountNO;//记录主箱号
                        account.RowId = "";
                        account.AccountNO = "";
                        account.UnpackingFlag = "new";
                        if (account.UnpackingFlag == "new") Accounts.Add(account);
                    }
                    else
                    {
                        account.Qty = (account.Qty.ToDouble() - dd_item.Qty.ToDouble()).ToString();
                        Accounts.Add(account);
                    }
                }

            });
            return Accounts;
        }


        /// <summary>
        /// 返还的业务逻辑
        /// </summary>
        private List<Account> Return(List<Account> OL_accounts, List<Account> DD_accounts, Account head, List<AccountType> accountType)
        {
            List<Account> Accounts = new List<Account>();
            //获取更新的数据
            OL_accounts.ForEach(account =>
            {
                //获取物料的资产类型信息
                List<AccountType> types = accountType.Where(t => t.Type == account.Type).ToList();
                AccountType type = types.Count() > 0 ? types[0] : new AccountType();
                var dd_item = DD_accounts.Where(x => x.AccountNO == account.AccountNO).First();
                account.DDEditBill = head.DDEditBill;//单据编号;
                account.DDEditType = head.DDEditType;//新建单据类型;
                account.Org = head.Org;//组织
                //如果需求数量与库存数量相等，不拆箱直接更新数据，否则插箱
                if (account.Qty.ToDouble() == dd_item.Qty.ToDouble())
                {
                    if (type.Inventory == "一物一码")
                    {
                        account.State = "闲置";
                        account.User = "";//接收人
                        account.Department = account.Department;//存放部门
                        account.GetDate = "";//领用日期
                        account.SavePosition = account.SavePosition;//存放地点
                        Accounts.Add(account);
                    }
                    else
                    {
                        account.Qty = (account.Qty.ToDouble() + dd_item.Qty.ToDouble()).ToString();
                        Accounts.Add(account);
                    }
                }
                else
                {
                    if (type.Inventory == "一物一码")
                    {
                        //需要操作的箱号数据
                        Account item = TRS_Unpacking(account, dd_item.Qty, head);
                        Accounts.Add(item);

                        //剩余数量的箱号数据
                        account.Qty = (account.Qty.ToDouble() - dd_item.Qty.ToDouble()).ToString();
                        account.MainBox = account.AccountNO;//记录主箱号
                        account.RowId = "";
                        account.AccountNO = "";
                        account.UnpackingFlag = "new";
                        if (account.UnpackingFlag == "new") Accounts.Add(account);
                    }
                    else
                    {
                        account.Qty = (account.Qty.ToDouble() + dd_item.Qty.ToDouble()).ToString();
                        Accounts.Add(account);
                    }
                }

            });
            return Accounts;
        }

        /// <summary>
        /// 维修的业务逻辑
        /// </summary>
        private List<Account> Maintenance(List<Account> OL_accounts, List<Account> DD_accounts, Account head, List<AccountType> accountType)
        {
            List<Account> Accounts = new List<Account>();
            //获取更新的数据
            OL_accounts.ForEach(account =>
            {
                var dd_item = DD_accounts.Where(x => x.AccountNO == account.AccountNO).First();
                account.DDEditBill = head.DDEditBill;//单据编号;
                account.DDEditType = head.DDEditType;//新建单据类型;
                //如果需求数量与库存数量相等，不拆箱直接更新数据，否则插箱
                if (account.Qty.ToDouble() == dd_item.Qty.ToDouble())
                {
                    account.State = "维修";
                    account.User = "";//接收人
                    account.Department = account.Department;//存放部门
                    account.GetDate = "";//领用日期
                    account.SavePosition = account.SavePosition;//存放地点
                    Accounts.Add(account);
                }
                else
                {
                    //需要操作的箱号数据
                    Account item = TRS_Unpacking(account, dd_item.Qty, head);
                    Accounts.Add(item);

                    //剩余数量的箱号数据
                    account.Qty = (account.Qty.ToDouble() - dd_item.Qty.ToDouble()).ToString();
                    account.MainBox = account.AccountNO;//记录主箱号
                    account.RowId = "";
                    account.AccountNO = "";
                    account.UnpackingFlag = "new";
                    if (account.UnpackingFlag == "new") Accounts.Add(account);
                }

            });
            return Accounts;
        }

        /// <summary>
        /// 报废的业务逻辑
        /// </summary>
        private List<Account> Scrap(List<Account> OL_accounts, List<Account> DD_accounts, Account head, List<AccountType> accountType)
        {
            List<Account> Accounts = new List<Account>();
            //获取更新的数据
            OL_accounts.ForEach(account =>
            {
                var dd_item = DD_accounts.Where(x => x.AccountNO == account.AccountNO).First();
                account.DDEditBill = head.DDEditBill;//单据编号;
                account.DDEditType = head.DDEditType;//新建单据类型;
                //如果需求数量与库存数量相等，不拆箱直接更新数据，否则插箱
                if (account.Qty.ToDouble() == dd_item.Qty.ToDouble())
                {
                    account.State = "报废";
                    account.User = "";//接收人
                    account.Department = account.Department;//存放部门
                    account.GetDate = "";//领用日期
                    account.SavePosition = account.SavePosition;//存放地点
                    Accounts.Add(account);
                }
                else
                {
                    //需要操作的箱号数据
                    Account item = TRS_Unpacking(account, dd_item.Qty, head);
                    Accounts.Add(item);

                    //剩余数量的箱号数据
                    account.Qty = (account.Qty.ToDouble() - dd_item.Qty.ToDouble()).ToString();
                    account.MainBox = account.AccountNO;//记录主箱号
                    account.RowId = "";
                    account.AccountNO = "";
                    account.UnpackingFlag = "new";
                    if (account.UnpackingFlag == "new") Accounts.Add(account);
                }

            });
            return Accounts;
        }


        /// <summary>
        /// 执行拆箱操作
        /// </summary>
        /// <param name="mainItem">主箱号</param>
        /// <param name="upackingQty">插箱数量</param>
        /// <param name="head">插箱的业务信息</param>
        /// <returns></returns>
        private Account TRS_Unpacking(Account mainItem, string upackingQty, Account head)
        {
            Account item = new Account();
            if (mainItem.Qty.ToDouble() >= mainItem.Qty.ToDouble())
            {
                //拆出来的箱用原来的
                item.RowId = mainItem.RowId;
                item.AccountNO = mainItem.AccountNO;
                item.Name = mainItem.Name;
                item.Model = mainItem.Model;
                item.Brand = mainItem.Brand;
                item.DDAddBill = mainItem.DDAddBill;
                item.DDAddType = mainItem.DDAddType;
                item.DDEditBill = mainItem.DDEditBill;
                item.DDEditType = mainItem.DDEditType;
                item.Type = mainItem.Type;
                item.TypeT = mainItem.TypeT;
                item.Unit = mainItem.Unit;
                item.IsFixed = mainItem.IsFixed;
                item.PurType = mainItem.PurType;
                item.PurLink = mainItem.PurLink;
                item.PurDate = mainItem.PurDate;
                item.Price = mainItem.Price;
                item.InvoicingType = mainItem.InvoicingType;
                item.Qty = upackingQty;//插箱数量
                //单体控件的数据
                if (head.State == "领用") item.State = "在用";
                else if (head.State == "归还") { item.State = "闲置"; }
                else if (head.State == "借用") item.State = "借用";
                else if (head.State == "维修") item.State = "维修";
                else if (head.State == "报废") item.State = "报废";
                else item.State = "其他";

                //如果设备状态为在用和借用，设置领用日期,否则清空领用日期及其他字段
                if (item.State == "在用" || item.State == "借用")
                {
                    item.User = head.User;//接收人
                    item.Department = head.Department.IsNullOrEmpty() ? "" : head.Department;//存放部门
                    item.GetDate = DateTime.Now.ToString("yyyy-MM-dd");//领用日期
                    item.SavePosition = head.Department.IsNullOrEmpty() ? "" : head.SavePosition;//存放地点
                }
                else
                {
                    item.User = "";
                    item.Department = head.Department.IsNullOrEmpty() ? "" : head.Department;//存放部门
                    item.GetDate = "";
                    item.SavePosition = head.Department.IsNullOrEmpty() ? "" : head.SavePosition;//存放地点
                }
            }
            return item;
        }
        #endregion

        /// <summary>
        /// 新增调拨操作的流水记录
        /// </summary>
        /// <param name="item"></param>
        /// <param name="folwType"></param>
        /// <param name="head"></param>
        private void AddFolw(List<Account> item, FolwType folwType, Account head)
        {
            List<Account> flow = new List<Account>();

            AccountFlowInterfaceBusiness accountFlowInterfaceBusiness = new AccountFlowInterfaceBusiness();

            item.ForEach(x =>
            {
                Account account = new Account();
                if (folwType == FolwType.采购)
                {
                    //account.State = "采购入库";
                    //account.DDAddBill = x.DDAddBill;
                    //account.DDAddType = x.DDAddBill;
                    //account.Qty = "1";
                    //account.Name = "";
                    //account.Model = "";
                    //flow.Add(x);
                }
                else if (folwType == FolwType.领用)
                {
                    account.State = "领用";
                    account.DDEditBill = x.DDEditBill;
                    account.DDEditType = x.DDEditType;
                    account.Qty = x.Qty;
                    account.AccountNO = x.AccountNO;
                    account.User = head.User;
                    flow.Add(account);
                }
                else if (folwType == FolwType.调拨)
                {
                    account.State = "调拨";
                    account.DDEditBill = x.DDEditBill;
                    account.DDEditType = x.DDEditType;
                    account.Qty = x.Qty;
                    account.AccountNO = x.AccountNO;
                    account.User = head.User;
                    flow.Add(account);
                }
                else if (folwType == FolwType.借用)
                {
                    account.State = "借用";
                    account.DDEditBill = x.DDEditBill;
                    account.DDEditType = x.DDEditType;
                    account.Qty = x.Qty;
                    account.AccountNO = x.AccountNO;
                    account.User = head.User;
                    flow.Add(account);
                }
                else if (folwType == FolwType.归还)
                {
                    account.State = "归还";
                    account.DDEditBill = x.DDEditBill;
                    account.DDEditType = x.DDEditType;
                    account.Qty = x.Qty;
                    account.AccountNO = x.AccountNO;
                    account.User = head.User_Put;
                    flow.Add(account);
                }
                else if (folwType == FolwType.报废)
                {
                    account.State = "报废";
                    account.DDEditBill = x.DDEditBill;
                    account.DDEditType = x.DDEditType;
                    account.Qty = x.Qty;
                    account.AccountNO = x.AccountNO;
                    account.User = head.User_Put;
                    flow.Add(account);
                }
                else if (folwType == FolwType.维修)
                {
                    account.State = "维修";
                    account.DDEditBill = x.DDEditBill;
                    account.DDEditType = x.DDEditType;
                    account.Qty = x.Qty;
                    account.AccountNO = x.AccountNO;
                    account.User = head.User_Put;
                    flow.Add(account);
                }
            });
            accountFlowInterfaceBusiness.AddRow(flow);

        }


        #region 查询
        /// <summary>
        /// 查询资产台账信息
        /// </summary>
        /// <param name="accountDTO"></param>
        /// <returns></returns>
        public async Task<Account> GetAccount(string accountNO)
        {
            Account account = new Account() { Name = "查询无结果" };
            if (!accountNO.IsNullOrEmpty())
            {
                List<OL_FilterDTO> filters = new List<OL_FilterDTO>();
                //设置ONE_link的查询条件：资产编码=Value的项
                OL_FilterDTO filter = new OL_FilterDTO() { ControlId = "6709cb88a23bbf6d2b165ec1", DataType = 2, FilterType = 2, Value = accountNO, SpliceType = 2 };
                filters.Add(filter);
                //根据资产编码查询对应的数据获取ROWID
                List<Account> accounts = await _AccountInterfaceBusiness.QueryRow(filters);
                account = accounts.Count > 0 && !accounts.IsNullOrEmpty() ? accounts[0] : account;
                account.Qty_Num = Convert.ToDouble(account.Qty);

            }
            return account;
        }


        /// <summary>
        /// 查询资产类别信息
        /// </summary>
        /// <param name="accountDTO"></param>
        /// <returns></returns>
        public async Task<List<AccountType>> GetAccountType(List<string> accountType)
        {
            List<AccountType> type = new List<AccountType>();
            if (!accountType.IsNullOrEmpty() && accountType.Count()>0)
            {
                List<OL_FilterDTO> filters = new List<OL_FilterDTO>();
                //设置ONE_link的查询条件：资产编码=Value的项
                foreach (var item1 in accountType)
                {
                    OL_FilterDTO filter = new OL_FilterDTO() { ControlId = "6711b352a23bbf6d2b316dc4", DataType = 2, FilterType = 2, Value = item1, SpliceType = 2 };
                    filters.Add(filter);
                }
               
                type = await _AccountTypeInterfaceBusiness.GetRows(filters);
                type=type.OrderBy(x => x.Order.ToInt()).ToList();
                //account = type.Count > 0 && !type.IsNullOrEmpty() ? type : account;
            }
            return type;
        }
        #endregion


    }

    enum FolwType
    {
        采购,
        领用,
        借用,
        归还,
        维修,
        报废,
        调拨,
        其他

    }
}